﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Quakk.Forms
{
    public partial class ViewPicture : Form
    {
        public Image Picture
        {
            get; set;
        }

        private Rectangle DisplayRectangle
        {
            get; set;
        }

        public ViewPicture()
        {
            InitializeComponent();
        }

        private void ViewPicture_Load(object sender, EventArgs e)
        {
            BackColor = Color.Black;

            CalculateDisplayRectangle();
        }

        private void CalculateDisplayRectangle()
        {
            if (Picture != null)
            {
                Rectangle rect;
                int width = Picture.Width;
                int height = Picture.Height;

                double widthMulti = (double) Width/width;
                double heightMulti = (double) Height/height;

                if (height*widthMulti > Height)
                {
                    width = Convert.ToInt32(width*heightMulti);
                    height = Height;
                    rect = new Rectangle((Width - width)/2, 0, width, height);
                }
                else
                {
                    height = Convert.ToInt32(height*widthMulti);
                    width = Width;
                    rect = new Rectangle(0, (Height - height)/2, width, height);
                }

                DisplayRectangle = rect;
            }
        }

        private void ViewPicture_Paint(object sender, PaintEventArgs e)
        {
            if (Picture != null)
            {
                e.Graphics.DrawImage(Picture, DisplayRectangle,
                                     new Rectangle(0, 0, Picture.Width, Picture.Height), GraphicsUnit.Pixel);
            }
        }

        private void ViewPicture_Resize(object sender, EventArgs e)
        {
            CalculateDisplayRectangle();
        }
    }
}